package com.hatic.contract;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.StringUtil;

import com.hatic.process.ProjectMoneyBO;

/**
 * 合同金额反馈项目金额信息。
 *
 * @author TonyTan
 * @version 1.0, 2011-10-29
 */
public class ContractBO extends BusinessObject {
	private static final long serialVersionUID = 1L;

	/**
	 * 合同文本提交时检查合同金额的有效性
	 * @param contractIds
	 * @return
	 */
	public String preTextAudit(String[] contractIds) {
		if (contractIds == null || contractIds.length == 0) {
			setMessage("没有选择合同记录！");
			return _returnFaild;
		}
		
		for (int i = 0, n = contractIds.length; i < n; i++) {
			if (!checkMoney(contractIds[i])) {
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}
	
	/**
	 * 合同签订记录提交时检查合同金额的有效性与更新项目中的合同金额
	 * 只有父合同反馈，子合同金额不反馈
	 * @param contractIds
	 * @return
	 */
	public String preWriteAudit(String[] contractIds) {
		if (contractIds == null || contractIds.length == 0) {
			setMessage("没有选择合同记录！");
			return _returnFaild;
		}
		
		for (int i = 0, n = contractIds.length; i < n; i++) {
			if (!writeContract(contractIds[i])) {
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}
	
	//提交合同文本时检查合同金额
	private boolean checkMoney(String contractId) {
		Map<String,String> mpData = queryContract(contractId);
		
		String projectId = mpData.get("project_id");
		String htmoney = mpData.get("contract_money");
		double money = Double.parseDouble(htmoney);
		_log.showDebug("---------项目ID=" + projectId + "; 合同金额：" + htmoney);
		
		//检查合同金额是否超出项目批复金额
		ProjectMoneyBO projectbo = new ProjectMoneyBO();
		boolean checked = projectbo.checkContractMoney(projectId, money);
		if (!checked) {
			String message = projectbo.getMessage();
			if (message == null || message.length() == 0) {
				message = "检查合同金额是否超额时出错！";
			}
			setMessage(message);
			return false;
		}
		
		return true;
	}
	
	//合同提交前检查与更新内容
	private boolean writeContract(String contractId) {
		Map<String,String> mpData = queryContract(contractId);
		
		String isChild = mpData.get("is_child");
		
		String projectId = mpData.get("project_id");
		String htmoney = mpData.get("contract_money");
		double money = Double.parseDouble(htmoney);
		_log.showDebug("---------项目ID=" + projectId + "; 合同金额：" + htmoney);
		
		//项目ID为空时，返回TRUE
		if(projectId == null || projectId.length() == 0) return true;
		
		//提交前检查，如果是子合同必须选择父合同；
		String conParentid = mpData.get("contract_parentid");
		if(StringUtil.tonum(isChild) == 1 && (conParentid ==null || conParentid.length() == 0)){
			setMessage("该合同为子合同，必须选择父合同！");
			return false;
		}
		
		//检查合同金额是否超出项目批复金额
		ProjectMoneyBO projectbo = new ProjectMoneyBO();
		boolean checked = projectbo.checkContractMoney(projectId, money);
		if (!checked) {
			String message = projectbo.getMessage();
			if (message == null || message.length() == 0) {
				message = "检查合同金额是否超额时出错！";
			}
			setMessage(message);
			return false;
		}
		
		//只有父合同反馈，子合同金额不反馈
		if(StringUtil.tonum(isChild) == 0  ){
			//更新项目、投资计划的合同总金额
			checked = projectbo.addContractMoney(projectId, money);
			if (!checked) {
				String message = projectbo.getMessage();
				if (message == null || message.length() == 0) {
					message = "更新项目中的合同金额出错！";
				}
				setMessage(message);
				return false;
			};
		}
		
		return true;
	}
	
	//查询合同信息
	private Map<String,String> queryContract(String contractId) {
		String sql = "select contract_money, pay_money, provider_id, project_id,is_child,contract_parentid from contract where contract_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(contractId);
		
		return _dao.queryMap(param);
	}
}
